<script setup lang="ts">
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { timeFormat, timeFormat_2 } from '@/hooks/util'
import * as imApi from '@/api/im'
import * as cardCouponsApi from '@/api/cardCoupons'

const myModal = ref()

let theTitle = ref<string>('请选择要取消关联的商品')

const showModal = (id: string) => {
  selectedRowKey.value = []
  selectedRow.value = []

  getList(id)
  myModal.value?.showModal()
}

const emit = defineEmits<{
  refresh: []
}>()

defineExpose({
  showModal,
})

const dataSource = ref<Array<any>>([])
const columns = [
  {
    title: '商品标题',
    key: 'title',
    width: 180,
  },
  {
    title: '商品描述',
    key: 'desc',
    width: 180,
  },
  {
    title: '商品id',
    key: 'fishProductId',
    width: 180,
  },
  {
    title: '商品状态',
    key: 'status',
    width: 180,
  },
]

// 表格多选
const selectedRowKey = ref<Array<any>>([])
const selectedRow = ref<Array<object>>([])
const onSelectChange = (selectedRowKeys: any[], selectedRows: any[]) => {
  console.log('selectedRowKey=', selectedRowKeys)
  // console.log('selectedRow=', selectedRow.value)
  selectedRowKey.value = selectedRowKeys
  selectedRow.value = selectedRows
}

const getList = async (cardRollId: string) => {
  try {
    let {
      state,
      data,
      message: msg,
    } = await cardCouponsApi.getRelevanceFishProduct({
      couponId: cardRollId,
      couponType: 1, // 1：星驰，2：91卡券
    })
    if (state === 200) {
      dataSource.value = data
    } else {
      message.error(msg)
    }
  } catch (error) {
    message.error('网络请求连接失败~')
  }
}

// 取消关联
const okCancelBind = async () => {
  const params = {
    itemIdList: selectedRowKey.value,
  }
  try {
    let { state, message: msg } = await cardCouponsApi.cardRollCancelRelevanceFishProduct(params)
    if (state === 200) {
      message.success('已解除关联')
      emit('refresh')
      myModal.value.cancelModal()
    } else {
      message.error(msg)
    }
  } catch (error) {
    message.error('网络请求连接失败~')
  }
}
</script>

<template>
  <MyModal ref="myModal" :title="theTitle" :top="25" :width="900">
    <template #content>
      <a-table
        style="overflow-x: scroll"
        :columns="columns"
        :data-source="dataSource"
        :pagination="false"
        rowKey="fishProductId"
        :row-selection="{ selectedRowKeys: selectedRowKey, onChange: onSelectChange }"
        :scroll="{ y: 300 }"
      >
        <template #emptyText>
          <a-empty description="暂无数据" />
        </template>

        <template #headerCell="{ column }"></template>

        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'title'">
            {{ record.title }}
          </template>

          <template v-else-if="column.key === 'desc'">
            {{ record.desc || '无' }}
          </template>

          <template v-else-if="column.key === 'fishProductId'">
            {{ record.fishProductId }}
          </template>

          <template v-else-if="column.key === 'status'">
            <a-tag color="blue" v-if="record.status == 0">上架</a-tag>
            <a-tag color="red" v-if="record.status == 1">已售出</a-tag>
            <a-tag color="red" v-if="record.status == -2">下架</a-tag>
            <a-tag color="red" v-if="record.status == -1">删除</a-tag>
            <a-tag color="red" v-if="record.status == -11">未认证</a-tag>
            <a-tag color="red" v-if="record.status == 99">其他</a-tag>
          </template>
        </template>
      </a-table>

      <div style="text-align: right; margin-top: 10px">
        <a-button @click="myModal.cancelModal()" style="border-radius: 6px">取消</a-button>
        <a-button
          @click="okCancelBind"
          style="margin-left: 10px; border-radius: 6px"
          type="primary"
        >
          确定取消关联
        </a-button>
      </div>
    </template>
  </MyModal>
</template>

<style scoped lang="less">
.imgBox {
  width: 50px;
  height: 50px;

  img {
    height: 50px;
  }
}

.popoverImg {
  width: 50px;
  img {
    width: 100%;
  }
}
</style>
